package com.situ.collection;

import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.FileFilter;
import java.util.*;

public class CollectionDemo {

    @Test
    public void test1() {
        Collection<String> collection = new ArrayList<>();
        collection.add("张三");
        collection.add("李四");
        collection.add("王五");
        collection.add("张三");
        for (String str : collection) {
            System.out.println(str);
        }
        Iterator<String> iterator = collection.iterator();
        while (iterator.hasNext()) {
            String data = iterator.next();
            System.out.println(data);
        }
    }

    @Test
    public void test2() {
        List<String> list = new ArrayList<String>();
        list.add("abc1");
        list.add("abc2");
        list.add("abc3");
        list.add("abc4");
        list.add(2, "Java");
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            String str = iterator.next();
            System.out.println(str);
        }
        System.out.println("-----------");
        for (int i = 0; i < list.size(); i++) {
            String str = list.get(i);
            System.out.println(str);
        }
        System.out.println("-----------");
        Iterator<String> it = list.iterator();
        while (it.hasNext()) {
            String str = it.next();
            if (str.equals("abc3")) {
                list.add("ABC3");
            }
            System.out.println(str);
        }
    }

    @Test
    public void ttt() {
        List<String> list = new ArrayList<String>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("5");
        for (String item : list) {
            if (item.equals("3")) {
                System.out.println(item);
                list.remove(item);
            }
        }
        System.out.println(list.size());
    }

    @Test
    public void ttt1() {
        List<String> list = new ArrayList<String>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("5");
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            String data = iterator.next();
            if (data.equals("3")) {
                System.out.println(data);
                iterator.remove();
            }
        }
        System.out.println(list.size());
    }

    @Test
    public void test551() {
        // 创建HashSet对象
        Set<String> set = new HashSet<String>();
        // 给集合中添加自定义对象
        set.add("zhangsan");
        set.add("lisi");
        set.add("wangwu");
        set.add("zhangsan");
        set.add(new String("lisi"));
        for (String string : set) {
            System.out.println(string);
        }
    }

    @Test
    public void test4() {
        Set<Student> set = new HashSet<>();
        Student student1 = new Student(1, "zhangsan");
        Student student2 = new Student(2, "lisi");
        Student student3 = new Student(3, "wangwu");
        Student student4 = new Student(1, "zhangsan");
        set.add(student1);
        set.add(student2);
        set.add(student3);
        set.add(student4);
        for (Student student : set) {
            System.out.println(student);
        }

    }

    @Test
    public void test41() {
        Set<String> set = new LinkedHashSet<>();
        set.add("bbb");
        set.add("aaa");
        set.add("abc");
        set.add("bbc");
        Iterator it = set.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
    }

    @Test
    public void test5() {
        TreeSet<Student> treeSet = new TreeSet<>();
        treeSet.add(new Student(1, "张三", 23));
        treeSet.add(new Student(2, "李四", 13));
        treeSet.add(new Student(3, "周七", 13));
        treeSet.add(new Student(4, "王五", 43));
        treeSet.add(new Student(5, "赵六", 33));

        System.out.println(treeSet);
    }

    @Test
    public void test1178() {
        File currentDirectory = new File("D:\\Java2407");
        //File[] files = currentDirectory.listFiles();
        File[] files = currentDirectory.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                return !pathname.getName().startsWith(".");
            }
        });

        Collections.sort(Arrays.asList(files), new Comparator<File>() {
            @Override
            public int compare(File file1, File file2) {
                if (file1.isDirectory() && file2.isFile()) {
                    return 1;
                } else if (file1.isFile() && file2.isDirectory()) {
                    return -1;
                } else {
                    return file1.getName().compareTo(file2.getName());
                }
            }
        });

        for (File file : files) {
            System.out.println(file);
        }
    }

    @Test
    public void test334() {
        System.out.println("Aa".hashCode());//2112
        System.out.println("BB".hashCode());//2112
    }

    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("5");
        list.add("6");
        list.add("7");
        list.add("8");
        list.add("9");
        list.add("10");
        list.add("11");

        Object o = new Object() {
            @Override
            public boolean equals(Object obj) {
                return true;
            }
        };
    }

}


